package org.jeecg.modules.demo.partner.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.demo.partner.entity.*;
import org.jeecg.modules.demo.partner.mapper.PartnerMapper;
import org.jeecg.modules.demo.partner.service.IPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements IPartnerService {

    @Autowired
    private PartnerMapper partnerMapper;

    public Boolean checkUserExists(String username){
        Boolean b =  false;

        if(partnerMapper.checkUserExists(username) || partnerMapper.checkUserExistsPartner(username)){
            b = true;
        }else {
            b = false;
        }
        return b;
    }

    public Boolean checkCompanyExists(String companyName){
        Boolean b =  false;
        if(partnerMapper.checkCompanyExists(companyName) || partnerMapper.checkCompanyExistsPartner(companyName)){
            b = true;
        }else {
            b = false;
        }
        return b;
    }

    public Partner getPartnerById(String id){
        return partnerMapper.getPartnerById(id);
    }

    @Transactional
    public void saveUser(SysUser user, String[] types){

        partnerMapper.updatePartnerById(user.getId());

        //角色
        for(String type:types){
            String roleId = "";
            if("2".equals(type)){
                roleId = "1959867223446286338";//保险合作商
            } else if("1".equals(type)){
                roleId = "1959867278391668737";//维修合作商
            } else if("3".equals(type)){
                roleId = "1959867331806130177";//年检合作商
            }
            SysUserRole userRole = new SysUserRole(user.getId(), roleId);
            userRole.setId(String.valueOf(IdWorker.getId()));

            partnerMapper.insertUserRole(userRole);
        }
        //部门
        String deaprtId = "*******";
        SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId);
        userDeaprt.setId(String.valueOf(IdWorker.getId()));
//        partnerMapper.insertUserDeaprt(userDeaprt);
        //租户
//        SysUserTenant relation = new SysUserTenant();
//        relation.setUserId(user.getId());
//        relation.setTenantId(Integer.valueOf("132"));
//        relation.setStatus(CommonConstant.STATUS_1);
//        relationMapper.insert(relation);

        //用户
        partnerMapper.insertUser(user);

    }

    public List<Partner> getPartnerByType(String cooperatorType){
        String sql = "SELECT * FROM GC_PARTNER WHERE COOPERATOR_TYPE LIKE '%"+cooperatorType+"%' AND STATUS = 1 order by CREATE_TIME DESC";
        return partnerMapper.getPartnerByType(sql);
    }
}
